package com.lhb.service.serviceImpl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.lhb.dto.StudentAddDTO;
import com.lhb.dto.StudentEditDTO;
import com.lhb.dto.StudentPageQueryDTO;
import com.lhb.entity.Student;
import com.lhb.mapper.StudentMapper;
import com.lhb.result.PageResult;
import com.lhb.service.StudentService;
import com.lhb.vo.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class StudentServiceImpl implements StudentService {

    @Autowired
    StudentMapper studentMapper;


    public PageResult pageQuery(StudentPageQueryDTO studentPageQueryDTO) {

        PageHelper.startPage(studentPageQueryDTO.getCurrentPage(), studentPageQueryDTO.getPageSize());

       Page<StudentVO> page = studentMapper.getStudent(studentPageQueryDTO);
        List<StudentVO> studentVOList = page.getResult();

       List<StudentPageQueryVO> studentPageQueryVOList = new ArrayList<>();
        for (StudentVO studentVO : studentVOList) {
            StudentPageQueryVO studentPageQueryVO = new StudentPageQueryVO();
         List<FamilyDetailVO>family=  studentMapper.getFamily(studentVO.getId());
         List<AwardDetailVO>award= studentMapper.getAward(studentVO.getId());
         BeanUtils.copyProperties(studentVO,studentPageQueryVO);
         studentPageQueryVO.setFamily(family);
         studentPageQueryVO.setAward(award);
         studentPageQueryVOList.add(studentPageQueryVO);
        }




        Long total = page.getTotal();



        return new PageResult(total, studentPageQueryVOList);
    }


    public StudentGetByIdVO getById(Long id) {
        Student student=  studentMapper.getStudentById(id);
        List<FamilyDetailVO> family = studentMapper.getFamily(id);
        List<AwardDetailVO> award = studentMapper.getAward(id);
        StudentGetByIdVO studentGetByIdVO = new StudentGetByIdVO();
        BeanUtils.copyProperties(student,studentGetByIdVO);
        studentGetByIdVO.setFamily(family);
        studentGetByIdVO.setAward(award);
        return studentGetByIdVO;
    }


    public void update(StudentEditDTO studentEditDTO) {


        studentMapper.updateStudent(studentEditDTO);

        for (FamilyDetailVO familyDetailVO : studentEditDTO.getFamily()) {
            familyDetailVO.setStudentId(studentEditDTO.getId());
            studentMapper.updateFamily(familyDetailVO);
        }
        List<Long> ids = new ArrayList<>();

        for (AwardDetailVO awardDetailVO : studentEditDTO.getAward()) {

            ids.add(studentEditDTO.getId());

        }
        studentMapper.deleteAward(ids);

        for (AwardDetailVO awardDetailVO : studentEditDTO.getAward()) {

            awardDetailVO.setStudentId(studentEditDTO.getId());
            studentMapper.saveAward(awardDetailVO);

        }

    }


    public void delete(List<Long> idList) {

        studentMapper.deleteStudent(idList);
        studentMapper.deleteFamily(idList);
        studentMapper.deleteAward(idList);
    }


    public void save(StudentAddDTO studentAddDTO) {

        Student student = new Student();
        BeanUtils.copyProperties(studentAddDTO,student);
        student.setEnrollDate(LocalDate.now());
        studentMapper.saveStudent(student);

        for (FamilyDetailVO familyDetailVO : studentAddDTO.getFamily()) {
            familyDetailVO.setStudentId(studentAddDTO.getId());
            studentMapper.saveFamily(familyDetailVO);
        }

        for (AwardDetailVO awardDetailVO : studentAddDTO.getAward()) {
            awardDetailVO.setStudentId(studentAddDTO.getId());
            studentMapper.saveAward(awardDetailVO);
        }


    }
}
